package dimensional.traveler.client.screens.welcome;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.event.logical.shared.AttachEvent.Handler;
import com.google.gwt.user.client.rpc.AsyncCallback;

import dimensional.traveler.client.UserManagement;
import dimensional.traveler.client.mvc.ControllerUtils;
import dimensional.traveler.client.mvc.MediatedController;
import dimensional.traveler.client.services.CareerService;
import dimensional.traveler.client.services.CareerServiceAsync;
import dimensional.traveler.client.services.ServiceAggregator;
import dimensional.traveler.client.services.ServiceAggregatorAsync;
import dimensional.traveler.client.ui.util.LoadingBar;
import dimensional.traveler.shared.SaveGameInfo;
import dimensional.traveler.shared.Settings;
import dimensional.traveler.shared.model.missionSequence.MissionSequence;

public class WelcomeScreenController extends MediatedController implements Handler {

	private ServiceAggregatorAsync serviceAggregator = GWT.create(ServiceAggregator.class);

	private CareerServiceAsync careerService = GWT.create(CareerService.class);

	private WelcomeScreen welcomeScreen;

	public void setWelcomeScreen(WelcomeScreen welcomeScreen) {
		this.welcomeScreen = welcomeScreen;
	}

	public void newGame() {
		careerService.startNewCareer(UserManagement.getLoggedInUser(), new AsyncCallback<MissionSequence>() {

			@Override
			public void onSuccess(MissionSequence firstMissionSequence) {
				mediator.displayMissionSequence(firstMissionSequence);
			}

			@Override
			public void onFailure(Throwable caught) {
				handleRequestFailure(caught);
			}
		});
	}

	public void logout() {
		ControllerUtils.logout(mediator, this);
	}

	public void continueGame() {
		careerService.loadLastGame(UserManagement.getLoggedInUser(), new AsyncCallback<Void>() {

			@Override
			public void onSuccess(Void result) {
				mediator.displayGameMenuScreen();
			}

			@Override
			public void onFailure(Throwable caught) {
				handleRequestFailure(caught);
			}
		});
	}

	public void loadGame() {
		ControllerUtils.loadGame(mediator, this);
	}

	@Override
	public void onAttachOrDetach(AttachEvent event) {
		if (event.isAttached()) {
			makeRequests();
		} else {
			this.welcomeScreen.stopShufflingImages();
		}
	}

	private void makeRequests() {
		LoadingBar.showLoadingBar();

		serviceAggregator.getMenuImagesAndSaveGameInfoForUser(UserManagement.getLoggedInUser(), new AsyncCallback<Map<String, Object>>() {
			@Override
			public void onSuccess(Map<String, Object> result) {
				@SuppressWarnings("unchecked")
				List<String> imagePaths = (List<String>) result.get(Settings.IMAGE_PATHS_KEY);
				processImagePaths(imagePaths);
				SaveGameInfo saveGameInfo = (SaveGameInfo) result.get(Settings.SAVE_GAME_INFO_KEY);
				processSaveGameInfo(saveGameInfo);

				LoadingBar.hideLoadingBar();
			}

			@Override
			public void onFailure(Throwable caught) {
				handleRequestFailure(caught);
			}
		});
	}

	private void processImagePaths(List<String> imagePaths) {
		welcomeScreen.addImagePaths(imagePaths);
		welcomeScreen.startShufflingImages();
	}

	private void processSaveGameInfo(SaveGameInfo saveGameInfo) {
		if (saveGameInfo.canContinue())
			welcomeScreen.continueButton.setEnabled(true);
		if (saveGameInfo.canLoadGame())
			welcomeScreen.loadGameButton.setEnabled(true);
	}

}
